package com.sky.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.DishFlavorsMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorsMapper dishFlavorsMapper;

    /**
     * 分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult findByPage(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize(), "update_time desc");
        Page<Dish> dishes = dishMapper.findByPage(dishPageQueryDTO);
        return new PageResult(dishes.getTotal(), dishes.getResult());
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Override
    public Dish findById(Integer id) {
        Dish dish = dishMapper.findById(id);
        dish.setUpdateTime(LocalDateTime.now());
        return dish;
    }

    /**
     * 菜品起售、停售
     *
     * @param status
     * @param id
     */
    @Override
    public void updateStatus(Integer status, Integer id) {
        dishMapper.updateStatus(status, id);
    }

    /**
     * 根据分类id查询菜品
     *
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> findByCategoryId(Integer categoryId) {
        List<Dish> dishes = dishMapper.findByCategoryId(categoryId);
        return dishes;
    }

    /**
     * 新增菜品
     *
     * @param dish
     */
    @Override
    @Transactional
    public void addDish(Dish dish) {
        //判断dish是否为空
        if (ObjectUtil.isNull(dish)) {
            return;
        }
        dish.setUpdateTime(LocalDateTime.now());
        dish.setCreateTime(LocalDateTime.now());
        Long dishId = dishMapper.addDish(dish);
        //拿到dishFlavors集合
        List<DishFlavor> flavors = dish.getFlavors();
        //判断flavors是否为空
        if (!ObjectUtil.isNull(flavors) && !ObjectUtil.isEmpty(flavors)) {
            flavors.forEach(flavor -> flavor.setDishId(dishId));
            dishFlavorsMapper.addFlavors(flavors);
        }
    }

    /**
     * 修改菜品
     *
     * @param dishDTO
     */
    @Override
    public void updateDish(DishDTO dishDTO) {
        dishMapper.updateDish(dishDTO);
        //判断dish.flavor是否为空
        List<DishFlavor> flavors = dishDTO.getFlavors();
        log.info("正在删除口味:{},", flavors);
        dishFlavorsMapper.deleteFlavor(dishDTO.getId());
        if (!ObjectUtil.isEmpty(flavors) && !ObjectUtil.isNull(flavors)) {
            flavors.forEach(flavor -> flavor.setDishId(dishDTO.getId()));
            dishFlavorsMapper.addFlavors(flavors);
        }
    }

    /**
     * 批量删除数据
     *
     * @param ids
     */
    @Override
    @Transactional
    public void deleteDishs(List<Integer> ids) {
        //判断ids是否为空
        if (!ObjectUtil.isNull(ids) && !ObjectUtil.isEmpty(ids)) {
            dishMapper.deleteDishs(ids);
            dishFlavorsMapper.deleteFlavors(ids);
        }
    }

    /**
     * C端根据分类id查询菜品
     *
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> findDish(Integer categoryId) {
        return dishMapper.findDish(categoryId);
    }

}
